var xyzdr = {
    setBtnStatus: function () {
        var row = $('#grid_xyzdr').datagrid('getSelected');
        if (row && row.id) {
            //未导入
            if (row.czzt == 'wdr') {
                if($("[name='xyzdr_drlx']:checked").val()==0){
                    $("#btn_xyzdr_insert").show();//导入
                    $("#btn_xyzdr_cx").hide();//撤销
                    $("#btn_xyzdr_cz").hide();//重置
                    $("#btn_xyzdr_qx").show();//取消
                }else{
                    $("#btn_xyzdr_insert").show();//导入
                    $("#btn_xyzdr_cx").hide();//撤销
                    $("#btn_xyzdr_cz").hide();//重置
                    $("#btn_xyzdr_qx").hide();//取消
                }
            }
            if (row.czzt == 'drcg') {
                $("#btn_xyzdr_insert").hide();//导入
                $("#btn_xyzdr_cx").show();//撤销
                $("#btn_xyzdr_cz").hide();//重置
                $("#btn_xyzdr_qx").hide();//取消
            }
            if (row.czzt == 'drsb') {
                $("#btn_xyzdr_insert").hide();//导入
                $("#btn_xyzdr_cx").hide();//撤销
                $("#btn_xyzdr_cz").show();//重置
                $("#btn_xyzdr_qx").hide();//取消
            }
            if (row.czzt == 'sccg') {
                $("#btn_xyzdr_insert").show();//导入
                $("#btn_xyzdr_cx").hide();//撤销
                $("#btn_xyzdr_cz").hide();//重置
                $("#btn_xyzdr_qx").show();//取消
            }
            if (row.czzt == 'scsb') {
                $("#btn_xyzdr_insert").hide();//导入
                $("#btn_xyzdr_cx").hide();//撤销
                $("#btn_xyzdr_cz").show();//重置
                $("#btn_xyzdr_qx").hide();//取消
            }
        } else {
            $("#btn_xyzdr_insert").hide();//导入
            $("#btn_xyzdr_cx").hide();//撤销
            $("#btn_xyzdr_cz").hide();//重置
            $("#btn_xyzdr_qx").hide();//取消
        }
    }
}

$(function () {
    if( $("[name='xyzdr_drlx']:checked").val()==0|| !$("[name='xyzdr_drlx']:checked").val()){
        //表格初始加载
        $('#grid_xyzdr').datagrid({
            url:$.webPath() + "/cwgl/xyzdr/xyzdrList",
            toolbar:"#toolbar_xyzdr",
            fitColumns:true,
            singleSelect:true,
            queryParams: {
                xyzdrDrlx: '0'
            },
            pagination:true,
            fit:true,
            pageNumber: 1,
            rownumbers:true,
            striped:true,
            columns:[[
                {field:'nbgs_jc', title:'公司名称', width:25,align: 'center'},
//                {field:'sfk_gldw_jc', title:'关联单位名称', width:25},
                {field:'xyzhm', title:'信用证号码', width:25,align: 'center'},
                {field:'dqrq', title:'到期日', width:30,align: 'center'},
                {field:'dqje', title:'到期金额', width:30,align: 'right',
                    formatter:function(value){
                        var sl = formatJe2(value,",");
                        return sl;
                    }
                },
                {field:'sfwffx', title:'付息方', width:30,align: 'center',
                    formatter:function(value){
                        if(value==0)
                            return '我方付息';
                        if(value==1)
                            return '卖方付息';
                    }
                },
                {field:'lxe', title:'利息额', width:30,align: 'right',
                    formatter:function(value){
                        var sl = formatJe2(value,",");
                        return sl;
                    }
                },
                {field:"jkrq" ,title:"借款日期",width:20,align: 'center'},
                {field:'createUser_xm', title:'制单人', width:15,align: 'center'},
                {field:'dateCreated', title:'制单日期', width:30,align: 'center'},
                {field:"kzf" ,title:"开证费",width:20,align: 'right',
                    formatter:function(value){
                        var sl = formatJe2(value,",");
                        return sl;
                    }},
                {field:'voucherNum', title:'凭证号', width:15,align: 'center'},
                {field: 'czzt', title: '状态', width: 10, align: 'center',formatter: function (value, row, index) {
                    if (value == 'wdr')
                        return "未导入";
                    if (value == 'drcg')
                        return "导入成功";
                    if (value == 'drsb')
                        return "导入失败";
                    if (value == 'sccg')
                        return "删除成功";
                    if (value == 'scsb')
                        return "删除失败";
                    return "";
                }}
            ]],
            onLoadSuccess:function(){
                var rows = $('#grid_xyzdr').datagrid("getRows");
                if(rows.length>0){
                    $('#grid_xyzdr').datagrid("selectRow", 0);
                }
                xyzdr.setBtnStatus();
                $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
            },
            onClickRow:function(){
                xyzdr.setBtnStatus();
            }
        });
    }else{
        //表格初始加载
        $('#grid_xyzdr').datagrid({
            url:$.webPath() + "/cwgl/xyzdr/xyzdrList",
            toolbar:"#toolbar_xyzdr",
            fitColumns:true,
            singleSelect:true,
            pagination:true,
            queryParams: {
                xyzdrDrlx: '1'
            },
            fit:true,
            pageNumber: 1,
            rownumbers:true,
            striped:true,
            columns:[[
                {field:'xyz_nbgs_jc', title:'公司名称', width:25},
//                {field:'xyz_sfk_gldw_jc', title:'关联单位名称', width:25},
                {field:'xyz_xyzhm', title:'信用证号码', width:25},
                {field:'xyz_dqrq', title:'到期日', width:30},
                {field:'xyz_dqje', title:'到期金额', width:30,
                    formatter:function(value){
                        var sl = formatJe2(value,",");
                        return sl;
                    }
                },
                {field:'xyz_sfwffx', title:'付息方', width:30,
                    formatter:function(value){
                        if(value==0)
                            return '我方付息';
                        if(value==1)
                            return '卖方付息';
                    }
                },
                {field:'xyz_lxe', title:'利息额', width:30,
                    formatter:function(value){
                        var sl = formatJe2(value,",");
                        return sl;
                    }
                },
                {field:"xyz_jkrq" ,title:"借款日期",width:20},
                {field:'createUser_xm', title:'制单人', width:15},
                {field:'dateCreated', title:'制单日期', width:30},
                {field:"xyz_kzf" ,title:"开证费",width:20,
                    formatter:function(value){
                        var sl = formatJe2(value,",");
                        return sl;
                    }},
                {field:'voucherNum', title:'凭证号', width:15},
                {field: 'czzt', title: '状态', width: 10, formatter: function (value, row, index) {
                    if (value == 'wdr')
                        return "未导入";
                    if (value == 'drcg')
                        return "导入成功";
                    if (value == 'drsb')
                        return "导入失败";
                    if (value == 'sccg')
                        return "删除成功";
                    if (value == 'scsb')
                        return "删除失败";
                    return "";
                }}
            ]],
            onLoadSuccess:function(){
                var rows = $('#grid_xyzdr').datagrid("getRows");
                if(rows.length>0){
                    $('#grid_xyzdr').datagrid("selectRow", 0);
                }
                xyzdr.setBtnStatus();
            },
            onClickRow:function(){
                xyzdr.setBtnStatus();
            }
       })
    }

    //当类型切换时切换页面
    $("input[name='xyzdr_drlx']").change(function () {
        if( $("[name='xyzdr_drlx']:checked").val()==0|| !$("[name='xyzdr_drlx']:checked").val()){
            //表格初始加载
            $('#grid_xyzdr').datagrid({
                url:$.webPath() + "/cwgl/xyzdr/xyzdrList",
                toolbar:"#toolbar_xyzdr",
                fitColumns:true,
                singleSelect:true,
                queryParams: {
                    xyzdrDrlx: '0'
                },
                pagination:true,
                fit:true,
                pageNumber: 1,
                rownumbers:true,
                striped:true,
                columns:[[
                    {field:'nbgs_jc', title:'公司名称', width:25},
                    {field:'sfk_gldw_jc', title:'关联单位名称', width:25},
                    {field:'xyzhm', title:'信用证号码', width:25},
                    {field:'dqrq', title:'到期日', width:30},
                    {field:'dqje', title:'到期金额', width:30,
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:'sfwffx', title:'付息方', width:30,
                        formatter:function(value){
                            if(value==0)
                                return '我方付息';
                            if(value==1)
                                return '卖方付息';
                        }
                    },
                    {field:'lxe', title:'利息额', width:30,
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:"jkrq" ,title:"借款日期",width:20},
                    {field:'createUser_xm', title:'制单人', width:15},
                    {field:'dateCreated', title:'制单日期', width:30},
                    {field:"kzf" ,title:"开证费",width:20,
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }},
                    {field:'voucherNum', title:'凭证号', width:15},
                    {field: 'czzt', title: '状态', width: 10, formatter: function (value, row, index) {
                        if (value == 'wdr')
                            return "未导入";
                        if (value == 'drcg')
                            return "导入成功";
                        if (value == 'drsb')
                            return "导入失败";
                        if (value == 'sccg')
                            return "删除成功";
                        if (value == 'scsb')
                            return "删除失败";
                        return "";
                    }}
                ]],
                onLoadSuccess:function(){
                    var rows = $('#grid_xyzdr').datagrid("getRows");
                    if(rows.length>0){
                        $('#grid_xyzdr').datagrid("selectRow", 0);
                    }
                    xyzdr.setBtnStatus();
                },
                onClickRow:function(){
                    xyzdr.setBtnStatus();
                }
            });
        }else{
            //表格初始加载
            $('#grid_xyzdr').datagrid({
                url:$.webPath() + "/cwgl/xyzdr/xyzdrList",
                toolbar:"#toolbar_xyzdr",
                fitColumns:true,
                singleSelect:true,
                queryParams: {
                    xyzdrDrlx: '1'
                },
                pagination:true,
                fit:true,
                pageNumber: 1,
                rownumbers:true,
                striped:true,
                columns:[[
                    {field:'xyz_nbgs_jc', title:'公司名称', width:25},
//                    {field:'xyz_sfk_gldw_jc', title:'关联单位名称', width:25},
                    {field:'xyz_xyzhm', title:'信用证号码', width:25},
                    {field:'xyz_dqrq', title:'到期日', width:30},
                    {field:'xyz_dqje', title:'到期金额', width:30,
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }
                    },
                    {field:'xyz_sfwffx', title:'付息方', width:30,
                        formatter:function(value){
                            if(value==0)
                                return '我方付息';
                            if(value==1)
                                return '卖方付息';
                        }
                    },
                    {field:'xyz_lxe', title:'利息额', width:30,
                        formatter:function(value){
                            var sl = formatJe(value,",");
                            return sl;
                        }
                    },
                    {field:"xyz_jkrq" ,title:"借款日期",width:20},
                    {field:'createUser_xm', title:'制单人', width:15},
                    {field:'dateCreated', title:'制单日期', width:30},
                    {field:"xyz_kzf" ,title:"开证费",width:20,
                        formatter:function(value){
                            var sl = formatJe2(value,",");
                            return sl;
                        }},
                    {field:'voucherNum', title:'凭证号', width:15},
                    {field: 'czzt', title: '状态', width: 10, formatter: function (value, row, index) {
                        if (value == 'wdr')
                            return "未导入";
                        if (value == 'drcg')
                            return "导入成功";
                        if (value == 'drsb')
                            return "导入失败";
                        if (value == 'sccg')
                            return "删除成功";
                        if (value == 'scsb')
                            return "删除失败";
                        return "";
                    }}
                ]],
                onLoadSuccess:function(){
                    var rows = $('#grid_xyzdr').datagrid("getRows");
                    if(rows.length>0){
                        $('#grid_xyzdr').datagrid("selectRow", 0);
                    }
                    xyzdr.setBtnStatus();
                },
                onClickRow:function(){
                    xyzdr.setBtnStatus();
                }
            })
        }
    });

    //**********按钮触发事件**********
    // 导入凭证按钮
    $("#btn_xyzdr_insert").click(function () {
        xyzdr_insert();
    });
    //撤销
    $("#btn_xyzdr_cx").click(function () {
        xyzdr_cx();
    });
    //重置
    $("#btn_xyzdr_cz").click(function () {
        xyzdr_cz();
    });
    //查询按钮
    $("#btn_xyzdr_search").click(function () {
        xyzdr_search();
    });
    //取消
    $("#btn_xyzdr_qx").click(function () {
        if($("[name='xyzdr_drlx']:checked").val()==1){
            $.messager.alert("提示","信用证费用不可取消", "error");
            return false;
        }
        xyzdr_qx();
    });

    //加载查询条件中的申报公司下拉框
    $("#xyzdr_gs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});

//删除付款审核
function xyzdr_cx(){
    var row = $('#grid_xyzdr').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否撤销该信用证？', function (r) {
            if (r) {
                var index = $('#grid_xyzdr').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/xyzdr/revoke_voucher?xyzdrId="+row.id+"&xyzdrDrlx="+$("[name='xyzdr_drlx']:checked").val(),
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_xyzdr").mask();
                    },
                    success:function(msg){
                        $("#layout_xyzdr").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_xyzdr').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        } else {
                            $('#grid_xyzdr').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要撤销的票据！", "warning");
    }
}


//删除付款审核
function xyzdr_cz(){
    var row = $('#grid_xyzdr').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否重置该信用证？', function (r) {
            if (r) {
                var index = $('#grid_xyzdr').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/xyzdr/rest_voucher?xyzdrId="+row.id+"&xyzdrDrlx="+$("[name='xyzdr_drlx']:checked").val(),
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_xyzdr").mask();
                    },
                    success:function(msg){
                        $("#layout_xyzdr").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_xyzdr').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        }else {
                            $('#grid_xyzdr').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的票据！", "warning");
    }
}

//贴现承兑取消
function xyzdr_qx(){
    var row = $('#grid_xyzdr').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否取消该信用证？', function (r) {
            if (r) {
                var index = $('#grid_xyzdr').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/xyzdr/revoke_note?xyzdrId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_xyzdr").mask();
                    },
                    success:function(msg){
                        $("#layout_xyzdr").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_xyzdr').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        }else {
                            $('#grid_xyzdr').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的票据！", "warning");
    }
}

//弹票据贴现窗口
function xyzdr_insert() {
    var row = $('#grid_xyzdr').datagrid('getSelected');
    if(row){
        var win = $.window({
            title:"信用证导入",
            width:1000,
            height:300,
            cache:false,
            url:$.webPath() + "/cwgl/xyzdr/xyzdr_insert?xyzdrId="+row.id+"&xyzdrDrlx="+$("[name='xyzdr_drlx']:checked").val(),
            modal:true,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $(win).mask();
                        $.ajax({
                            url:$.webPath() + "/cwgl/xyzdr/generate_voucher",
                            dataType: 'json',
                            type: 'POST',
                            data:{"xyzdrSystemDate":$("#xyzdr_systemDate").datebox("getValue"),
                                "attachment":$("#xyzdr_attachment").val(),
                                "bz":$("#xyzdr_bz").val(),
                                "xyzdrId":$("#xyzdr_id").val(),
                                "xyzdrDrlx": $("[name='xyzdr_drlx']:checked").val()},
                            beforeSend: function () {
                              if($("#xyzdr_attachment").val()=="" || $("#xyzdr_attachment").val()==null){
                                  $.messager.alert("提示","请填写附单据数", "error");
                                  $(win).mask('hide');
                                  return false;
                              }
                                if($("#xyzdr_systemDate").datebox("getValue")=="" || $("#xyzdr_systemDate").datebox("getValue")==null){
                                    $.messager.alert("提示","请填写制单时间", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                            },
                            success:function(msg){
                                $(win).mask('hide');
                                $(win).dialog('close');
                                if (msg.responseflag == 0){
                                    $('#grid_xyzdr').datagrid('reload');
                                    $.messager.alert("提示", msg.responsemessage, "error");
                                }else {
                                    $('#grid_xyzdr').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: msg.responsemessage,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                }
                            }
                        });

                    }
                }
            ]
        });
    }else{
        $.messager.alert('提示', '请选择一行数据', 'error');
    }
}

//查询销售单结算
function xyzdr_search() {
    var gsId = $('#xyzdr_gs').combotree('getValue') == null ? null : $('#xyzdr_gs').combotree('getValue');
    var xyzdrZt = $('#xyzdr_zt').combobox('getValue') == null ? null : $('#xyzdr_zt').combobox('getValue');
    $('#grid_xyzdr').datagrid('load',
        {
            jsdh:$('#xyzdr_jsdh').val(),
            gsId:gsId,
            xyzdrZt:xyzdrZt,
            voucherNum:$("#xyzdr_pzh").val(),
            czr:$("#xyzdr_czr").val(),
            xyzhm:$("#xyzdr_xyzhm").val(),
            xyzdrDrlx:$("[name='xyzdr_drlx']:checked").val()
        }
    );
}
